package com.football;

import data.FootBallMemberConstants;
import model.FootBallMember;

import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.util.*;
import java.util.List;

/**
 * 未每个单元格添加修改button按钮
 */
class TableUpdateRender extends JButton implements TableCellRenderer {
    private DispMemberFrame frame;
    public TableUpdateRender(DispMemberFrame frame) {
        this.frame = frame;
    }

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
            int row, int column) {

        this.setText("update");
        if (isSelected) {
            String name = (String) table.getValueAt(row,0);
            List<FootBallMember> members = FootBallMemberConstants.getFootBallMembers();
            FootBallMember result = null;
            //获取点击修改的单元格的球员
            for (FootBallMember footBallMember : members){
                String currentName = footBallMember.getName();
                if (currentName.equals(name)){
                    result = footBallMember;
                    break;
                }
            }
            //table 列表隐藏
            frame.setVisible(false);
            UpdateMemberFrame updateMemberFrame = new UpdateMemberFrame(result);
            updateMemberFrame.setVisible(true);

        }
        return this;
    }
}